﻿using Database.Context;
using QuanLyThongTinCoSo.BaseForm;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;
using Model = Database.Models;

namespace QuanLyThongTinCoSo.Forms.PhieuDieuTraCoSo
{
    public partial class frmTuyenTruyenHuanLuyen : frmDetail
    {
        public frmTuyenTruyenHuanLuyen()
        {
            InitializeComponent();
            Text = @"Công tác tuyên truyền huyến luyện PCCC";
        }

        public frmTuyenTruyenHuanLuyen(int id)
        {
            InitializeComponent();
            Text = @"Sửa Thẩm duyệt PCCC";
            dtNgay.Focus();
            _fromtype = Constant.TypeForm.Edit;
            _id = id;

            var biz = new TuyenTruyenHuanLuyenBiz();
            var item = biz.GetItemById(id) as Model.TuyenTruyenHuanLuyen;
            if (item == null) return;

            dtNgay.Value = item.Ngay;
            txtThoiLuong.Value = item.ThoiLuong;
            txtNoiDung.Text = item.NoiDung;
            txtLucLuongPCCC.Value = item.LucLuongPCCCCoSo;
            txtCanBo.Value = item.CanBoQuanLy;
            txtNguoiLaoDong.Value = item.NguoiLaoDong;
            txtDoiTuongKhac.Value = item.DoiTuongKhac;
            txtSoGiay.Text = item.SoGiayCN;
        }

        #region Validation

        protected override bool Validation()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtNoiDung.Text))
            {
                errorProvider.SetError(txtNoiDung, "Bạn chưa nhập nội dung huấn luyện. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }

        #endregion

        #region Insert

        protected override void Insert()
        {
            var context = new Context();
            var model = new Model.TuyenTruyenHuanLuyen
            {
                Ngay = dtNgay.Value,
                ThoiLuong = (int) txtThoiLuong.Value,
                NoiDung = txtNoiDung.Text.Trim(),
                LucLuongPCCCCoSo = (int) txtLucLuongPCCC.Value,
                CanBoQuanLy = (int) txtCanBo.Value,
                NguoiLaoDong = (int) txtNguoiLaoDong.Value,
                DoiTuongKhac = (int) txtDoiTuongKhac.Value,
                SoGiayCN = txtSoGiay.Text.Trim()
            };

            var biz = new TuyenTruyenHuanLuyenBiz(context);
            biz.Insert(model);
        }

        #endregion

        #region Edit

        protected override void Edit()
        {
            var context = new Context();
            var biz = new TuyenTruyenHuanLuyenBiz(context);
            var item = biz.GetItemById(_id) as Model.TuyenTruyenHuanLuyen;
            if (item != null)
            {
                item.Ngay = dtNgay.Value;
                item.ThoiLuong = (int) txtThoiLuong.Value;
                item.NoiDung = txtNoiDung.Text.Trim();
                item.LucLuongPCCCCoSo = (int) txtLucLuongPCCC.Value;
                item.CanBoQuanLy = (int) txtCanBo.Value;
                item.NguoiLaoDong = (int) txtNguoiLaoDong.Value;
                item.DoiTuongKhac = (int) txtDoiTuongKhac.Value;
                item.SoGiayCN = txtSoGiay.Text.Trim();
                biz.Update(item);
            }
        }

        #endregion
    }
}